package com.ztgf.commodity.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.base.Joiner;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.models.bo.CategoryImportBO;
import com.ztgf.commodity.models.bo.CategoryImportSaveBO;
import com.ztgf.commodity.models.bo.CategoryImportValueBO;
import com.ztgf.commodity.models.bo.CategoryMaintainNewSaveBO;
import com.ztgf.commodity.models.dto.*;
import com.ztgf.commodity.models.service.*;
import com.ztgf.commodity.models.vo.CategoryAttributesBean;
import com.ztgf.commodity.models.vo.CategoryBrandAttributesVO;
import com.ztgf.commodity.models.vo.CategoryParamBean;
import com.ztgf.commodity.models.vo.CommoditySpecificationVO;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.common.utils.PinYinUtils;
import com.ztgf.db.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.text.DecimalFormat;
import java.util.*;

import static com.ztgf.commodity.models.enums.CategoryImportUtil.*;
import static com.ztgf.commodity.models.enums.CategoryImportUtil.LABEL_ATTRIBUTES;
import static com.ztgf.common.utils.UtilConstants.*;

@Slf4j
@DubboService
public class CategoryMaintainServiceImpl implements ICategoryMaintainService {

    @Resource
    private YmlConfig ymlConfig;
    @DubboReference
    private ICategoryBrandAttributesRelationService categoryBrandAttributesRelationService;
    @DubboReference
    private ICommoditySpecificationService commoditySpecificationService;
    @DubboReference
    private ICommoditySpecificationAttrService commoditySpecificationAttrService;
    @DubboReference
    private ICommodityParamService commodityParamService;
    @DubboReference
    private ICommodityParamValueService commodityParamValueService;
    @DubboReference
    private ICommodityUnitService commodityUnitService;
    @DubboReference
    private ICommodityLabelService commodityLabelService;
    @DubboReference
    private ICommodityCategoryService commodityCategoryService;
    @DubboReference
    private IBrandInfoService brandInfoService;
    @DubboReference
    private ICategoryMaintainRecordService categoryMaintainRecordService;
    @DubboReference
    private ICategoryImportService categoryImportService;
    @Autowired
    private RedisUtil redisUtil;
    @DubboReference
    private ICategoryImportErrorDateService categoryImportErrorDateService;
    // redisKEY
    private static final String CATEGORY_IMPORT_KEY = "CATEGORY_IMPORT_KEY";

    @Override
    public CategoryAttributesBean queryCategoryBrandAttributes(Long categoryId, Long brandId) {
        // 先获取类目品牌关联表数据
        Wrapper<CategoryBrandAttributesRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("category_id", categoryId).eq("brand_id", brandId);
        CategoryBrandAttributesRelationDTO selectOne = categoryBrandAttributesRelationService.selectOne(wrapper);
        if (null == selectOne) {
            return null;
        }
        CategoryAttributesBean result = new CategoryAttributesBean();
        // 销售属性
        List<CommoditySpecificationVO> salesAttributeCollection = new ArrayList<>();
        if (StringUtils.isNotBlank(selectOne.getSpecificationIds())) {
            String[] salesIds = selectOne.getSpecificationIds().split(",");
            for (String id : salesIds) {
                CommoditySpecificationDTO select = commoditySpecificationService.selectById(id);
                if (select != null) {
                    // 查询销售属性值
                    Wrapper<CommoditySpecificationAttrDTO> wrapperA = new EntityWrapper<>();
                    wrapperA.eq("specification_id", select.getId());
                    List<CommoditySpecificationAttrDTO> selectList = commoditySpecificationAttrService.selectList(wrapperA);
                    CommoditySpecificationVO vo = new CommoditySpecificationVO();
                    vo.setId(select.getId());
                    vo.setName(select.getName());
                    if (StringUtils.isNotBlank(select.getNotes())) {
                        vo.setNotes(select.getNotes());
                    }
                    if (!isNull(selectList)) {
                        vo.setAttrList(selectList);
                    }
                    salesAttributeCollection.add(vo);
                }
            }
            result.setSalesAttributeCollection(salesAttributeCollection);
        }
        // 参数属性
        List<CategoryParamBean> parameterAttributeCollection = new ArrayList<>();
        if (StringUtils.isNotBlank(selectOne.getParamIds())) {
            String[] parameterIds = selectOne.getParamIds().split(",");
            for (String id : parameterIds) {
                CommodityParamDTO select = commodityParamService.selectById(id);
                if (select != null) {
                    // 查询参数值
                    Wrapper<CommodityParamValueDTO> wrapperB = new EntityWrapper<>();
                    wrapperB.eq("param_id", select.getId());
                    List<CommodityParamValueDTO> selectList = commodityParamValueService.selectList(wrapperB);
                    CategoryParamBean bean = new CategoryParamBean();
                    bean.setId(select.getId());
                    bean.setParamName(select.getParamName());
                    if (StringUtils.isNotBlank(select.getNotes())) {
                        bean.setNotes(select.getNotes());
                    }
                    if (!isNull(selectList)) {
                        bean.setValueList(selectList);
                    }
                    parameterAttributeCollection.add(bean);
                }
            }
            result.setParameterAttributeCollection(parameterAttributeCollection);
        }
        // 单位属性
        List<CommodityUnitDTO> unitCollection = new ArrayList<>();
        if (StringUtils.isNotBlank(selectOne.getUnitIds())) {
            String[] unitIds = selectOne.getUnitIds().split(",");
            for (String id : unitIds) {
                CommodityUnitDTO select = commodityUnitService.selectById(id);
                if (select != null) {
                    unitCollection.add(select);
                }
            }
            result.setUnitCollection(unitCollection);
        }
        // 标签属性
        List<CommodityLabelDTO> labelCollection = new ArrayList<>();
        if (StringUtils.isNotBlank(selectOne.getLabelIds())) {
            String[] labelIds = selectOne.getLabelIds().split(",");
            for (String id : labelIds) {
                CommodityLabelDTO select = commodityLabelService.selectById(id);
                if (select != null) {
                    labelCollection.add(select);
                }
            }
            result.setLabelCollection(labelCollection);
        }
        return result;
    }

    @Override
    public void insertCategoryMaintenance(List<CategoryMaintainNewSaveBO> list) {
        try {
            for (CategoryMaintainNewSaveBO saveBO : list) {
                Long id;
                if (saveBO.getCategoryId() != null) {
                    id = saveBO.getCategoryId();
                } else if (saveBO.getTwoCategoryId() != null) {
                    id = saveBO.getTwoCategoryId();
                } else {
                    id = saveBO.getOneCategoryId();
                }
                // 获取类目名称
                CommodityCategoryDTO category = commodityCategoryService.selectById(id);
                // 获取品牌名称
                BrandInfoDTO brandInfo = brandInfoService.selectById(saveBO.getBrandId());
                // 先查询类目品牌属性是否为空
                Wrapper<CategoryBrandAttributesRelationDTO> wrapper = new EntityWrapper<>();
                wrapper.eq("category_id", id).eq("brand_id", saveBO.getBrandId());
                CategoryBrandAttributesRelationDTO selectOne = categoryBrandAttributesRelationService.selectOne(wrapper);
                Map<String, String> map;
                if (selectOne == null) {
                    map = this.add(saveBO, category.getName(), brandInfo.getName(), id);
                } else {
                    map = this.update(selectOne, saveBO, category.getName(), brandInfo.getName());
                }
                if (saveBO.getId() == null) {
                    // 保存类目维护表
                    CategoryMaintainRecordDTO insertA = new CategoryMaintainRecordDTO();
                    insertA.setFirstCategoryId(saveBO.getOneCategoryId());
                    if (saveBO.getTwoCategoryId() != null) {
                        insertA.setTwoCategoryId(saveBO.getTwoCategoryId());
                    }
                    if (saveBO.getCategoryId() != null) {
                        insertA.setThreeCategoryId(saveBO.getCategoryId());
                    }
                    insertA.setBrandId(saveBO.getBrandId());
                    insertA.setSpecificationIds(map.get("salesIds"));
                    insertA.setParamIds(map.get("parameterIds"));
                    insertA.setUnitIds(map.get("unitIds"));
                    insertA.setLabelIds(map.get("labelIds"));
                    insertA.setId(ymlConfig.getPkId());
                    categoryMaintainRecordService.insert(insertA);
                } else {
                    // 修改类目维护表
                    CategoryMaintainRecordDTO updateDto = new CategoryMaintainRecordDTO();
                    updateDto.setFirstCategoryId(saveBO.getOneCategoryId());
                    if (saveBO.getTwoCategoryId() != null) {
                        updateDto.setTwoCategoryId(saveBO.getTwoCategoryId());
                    }
                    if (saveBO.getCategoryId() != null) {
                        updateDto.setThreeCategoryId(saveBO.getCategoryId());
                    }
                    updateDto.setBrandId(saveBO.getBrandId());
                    updateDto.setSpecificationIds(map.get("salesIds"));
                    updateDto.setParamIds(map.get("parameterIds"));
                    updateDto.setUnitIds(map.get("unitIds"));
                    updateDto.setLabelIds(map.get("labelIds"));
                    updateDto.setId(saveBO.getId());
                    categoryMaintainRecordService.updateById(updateDto);
                }
            }
        } catch (Exception e) {
            log.info("报错类目维护错误信息:{}", e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    public boolean importCategoryAttributes(byte[] input, String fileName) {
        // 先保存导入文件的信息
        Long id = ymlConfig.getPkId();
        CategoryImportDTO insertDto = new CategoryImportDTO();
        insertDto.setId(id);
        insertDto.setName(fileName);
        categoryImportService.insert(insertDto);
        // 解析excel
        // 将文件byte转成inputStream
        ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
        try {
            Workbook workbook = WorkbookFactory.create(inputStream);
            inputStream.close();
            // 获取工作表对象
            Sheet sheet = workbook.getSheetAt(0);
            // 获取Excel表格中数据的行数
            int totalRow = sheet.getPhysicalNumberOfRows();
            // 获取Excel表格中数据的列数
            int totalCell = sheet.getRow(0).getPhysicalNumberOfCells();
            // 第一行
            Row oneRow = sheet.getRow(0);
            // 解析完成后的全部成功数据
            List<CategoryImportSaveBO> trueList = new ArrayList<>();
            List<CategoryImportSaveBO> errorList = new ArrayList<>();
            for (int i = 1; i <= totalRow; i++) {
                // 获取第i行
                Row row = sheet.getRow(i);
                if (null == row || "".equals(row)) {
                    totalRow--;
                    continue;
                }
                String check = this.getCellValue(row.getCell(0));
                if (!StringUtils.isNotBlank(check)) {
                    totalRow--;
                    continue;
                }
                // 序号
                String orderNum = this.getCellValue(row.getCell(0));
                // 一行正确的数据
                CategoryImportSaveBO correctObject = new CategoryImportSaveBO();
                correctObject.setOrderNumber(orderNum);
                correctObject.setImportId(id);
                try{
                    for (int j = 1; j <= totalCell; j++) {
                        // 获取标题
                        String title = this.getCellValue(oneRow.getCell(j)).trim();
                        // 获取单元格值并去除空格
                        String value = this.getCellValue(row.getCell(j)).trim();
                        if (CustomizeUtil.strIsNull(value)) {
                            continue;
                        }
                        // 判断值是否包含非法字符
                        boolean checkValue = value.contains("！");
                        switch (title) {
                            case ZERO_CATEGORY_NAME:
                                correctObject.setZeroCategoryName(value);
                                break;
                            case ONE_CATEGORY_NAME:
                                correctObject.setOneCategoryName(value);
                                break;
                            case TWO_CATEGORY_NAME:
                                correctObject.setTwoCategoryName(value);
                                break;
                            case THREE_CATEGORY_NAME:
                                correctObject.setThreeCategoryName(value);
                                break;
                            case BRAND:
                                correctObject.setBrandName(value);
                                break;
                            case UNIT:
                                log.info("导入类目单位标题");
                                correctObject.setUnits(value);
                                if (checkValue) {
                                    correctObject.setIsCheck(1);
                                }
                                break;
                            case SALES_ATTRIBUTES:
                                correctObject.setSales(value);
                                if (checkValue) {
                                    correctObject.setIsCheck(1);
                                }
                                break;
                            case PARAM_ATTRIBUTES:
                                correctObject.setParameter(value);
                                if (checkValue) {
                                    correctObject.setIsCheck(1);
                                }
                                break;
                            case LABEL_ATTRIBUTES:
                                correctObject.setLabels(value);
                                if (checkValue) {
                                    correctObject.setIsCheck(1);
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }catch (Exception e) {
                    log.info("correctObject", e.getMessage());
                    errorList.add(correctObject);
                    //e.printStackTrace();
                    continue;
                }
                    trueList.add(correctObject);
//                if (correctObject.getIsCheck() == 0) {
//                    trueList.add(correctObject);
//                } else {
//                    errorList.add(correctObject);
//                }
            }
            // 异步将错误数据保存到数据库
            if (CustomizeUtil.listIsNotNull(errorList)) {
                this.addImportErrorDate(errorList, id);
            }
            // 将成功数据放入redis
            Object importDate = JSONObject.toJSON(trueList);
            boolean set = redisUtil.set(CATEGORY_IMPORT_KEY, importDate);
            if (!set) {
                CategoryImportDTO updateDto = new CategoryImportDTO();
                updateDto.setId(id);
                updateDto.setStatus(2);
                categoryImportService.updateById(updateDto);
                return false;
            }
            return true;
        } catch (Exception e) {
            log.info("类目导入报错信息:{}", e.getMessage());
            e.printStackTrace();
        }
        CategoryImportDTO updateDto = new CategoryImportDTO();
        updateDto.setId(id);
        updateDto.setStatus(2);
        categoryImportService.updateById(updateDto);
        return false;
    }

    @Async
    @Override
    public void addImportDate() {
        Object date = redisUtil.get(CATEGORY_IMPORT_KEY);
        log.info("导入的正确数据:{}", date);
        List<CategoryImportSaveBO> list = JSONObject.parseArray(JSONObject.toJSONString(date), CategoryImportSaveBO.class);
        if (isNull(list)) {
            log.info("导入的正确数据是空的");
            return;
        }
       // try {
            this.handleCategoryImportDate(list);

//        } catch (Exception e) {
//            CategoryImportDTO updateDto = new CategoryImportDTO();
//            updateDto.setId(list.get(0).getImportId());
//            updateDto.setStatus(2);
//            categoryImportService.updateById(updateDto);
//            log.info("类目导入正确数据处理失败，失败信息:{}", e.getMessage());
//            redisUtil.del(CATEGORY_IMPORT_KEY);
//            e.printStackTrace();
//        }
    }

    /**
     * @Description: 异步将错误数据保存到数据库
     * @Author: chenyu.wang
     * @Params: [listError, id]
     * @Date: 2020.12.30 16:33
     */
    @Async
    public void addImportErrorDate(List<CategoryImportSaveBO> listError, Long id) {
        if (isNull(listError)) {
            return;
        }
        for (CategoryImportSaveBO importBO : listError) {
            CategoryImportErrorDateDTO insertDto = new CategoryImportErrorDateDTO();
            insertDto.setImportId(id);
            if (CustomizeUtil.strIsNotNull(importBO.getOrderNumber())) {
                insertDto.setOrderNumber(importBO.getOrderNumber());
            }
            if (CustomizeUtil.strIsNotNull(importBO.getZeroCategoryName())) {
                insertDto.setZeroCategory(importBO.getZeroCategoryName());
            }
            if (CustomizeUtil.strIsNotNull(importBO.getOneCategoryName())) {
                insertDto.setOneCategory(importBO.getOneCategoryName());
            }
            if (CustomizeUtil.strIsNotNull(importBO.getTwoCategoryName())) {
                insertDto.setTwoCategory(importBO.getTwoCategoryName());
            }
            if (CustomizeUtil.strIsNotNull(importBO.getThreeCategoryName())) {
                insertDto.setSanCategory(importBO.getThreeCategoryName());
            }
            if (CustomizeUtil.strIsNotNull(importBO.getBrandName())) {
                insertDto.setBrandName(importBO.getBrandName());
            }
            if (CustomizeUtil.strIsNotNull(importBO.getUnits())) {
                insertDto.setUnitName(importBO.getUnits());
            }
            if (CustomizeUtil.strIsNotNull(importBO.getSales())) {
                insertDto.setSaleAttribute(importBO.getSales());
            }
            if (CustomizeUtil.strIsNotNull(importBO.getParameter())) {
                insertDto.setParamAttribute(importBO.getParameter());
            }
            if (CustomizeUtil.strIsNotNull(importBO.getLabels())) {
                insertDto.setLabelAttribute(importBO.getLabels());
            }
            insertDto.setId(ymlConfig.getPkId());
            log.info("类目导入的错误信息:{}", insertDto);
            categoryImportErrorDateService.insert(insertDto);
        }
    }


    /**
     * @return : void
     * @Author :chenyu.wang
     * @Description : 检查类目时添加的错误信息
     * @Date : 17:43 2021.01.18
     * @Param : [listError, id]
     **/
    @Async
    public void addOneImportErrorDate(CategoryImportSaveBO listError, Long id) {
        try {
            CategoryImportErrorDateDTO insertDto = new CategoryImportErrorDateDTO();
            insertDto.setImportId(id);
            if (CustomizeUtil.strIsNotNull(listError.getOrderNumber())) {
                insertDto.setOrderNumber(listError.getOrderNumber());
            }
            if (CustomizeUtil.strIsNotNull(listError.getZeroCategoryName())) {
                insertDto.setZeroCategory(listError.getZeroCategoryName());
            }
            if (CustomizeUtil.strIsNotNull(listError.getOneCategoryName())) {
                insertDto.setOneCategory(listError.getOneCategoryName() + "类目原本是最终级且存在属性");
            }
            if (CustomizeUtil.strIsNotNull(listError.getTwoCategoryName())) {
                insertDto.setTwoCategory(listError.getTwoCategoryName() + "类目原本是最终级且存在属性");
            }
            if (CustomizeUtil.strIsNotNull(listError.getThreeCategoryName())) {
                insertDto.setSanCategory(listError.getThreeCategoryName() + "类目原本是最终级且存在属性");
            }
            if (CustomizeUtil.strIsNotNull(listError.getBrandName())) {
                insertDto.setBrandName(listError.getBrandName());
            }
            if (CustomizeUtil.strIsNotNull(listError.getUnits())) {
                insertDto.setUnitName(listError.getUnits());
            }
            if (CustomizeUtil.strIsNotNull(listError.getSales())) {
                insertDto.setSaleAttribute(listError.getSales());
            }
            if (CustomizeUtil.strIsNotNull(listError.getParameter())) {
                insertDto.setParamAttribute(listError.getParameter());
            }
            if (CustomizeUtil.strIsNotNull(listError.getLabels())) {
                insertDto.setLabelAttribute(listError.getLabels());
            }
            insertDto.setId(ymlConfig.getPkId());
            categoryImportErrorDateService.insert(insertDto);
        } catch (Exception e) {
            log.info("检查类目时添加的错误信息:{}", e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * @Description: 处理解析完成的类目导入数据
     * @Author: chenyu.wang
     * @Params: [list]
     * @Date: 2020.12.16 15:17
     */
    private void handleCategoryImportDate(List<CategoryImportSaveBO> list) {
        if (isNull(list)) {
            return;
        }
        List<CategoryImportSaveBO> errorList = new ArrayList<>();
        List<CategoryImportSaveBO> trueList = new ArrayList<>();
        for (CategoryImportSaveBO correctObject : list) {
            if (correctObject.getIsCheck() == 0) {
                trueList.add(correctObject);
            } else {
                errorList.add(correctObject);
            }
        }
             if (trueList!=null && trueList.size()>0)  {
                 for (CategoryImportSaveBO bo : trueList) {
                     try {
                         // 类目
                         Long categoryId = this.processingCategory(bo);
                         log.info("类目导入类目id:{}", categoryId);
                         // 判断类目是否是最终极且包含属性
                         if (categoryId.equals(0L)) {
                             this.addOneImportErrorDate(bo, bo.getImportId());
                             continue;
                         }
                         // 查询类目
                         CommodityCategoryDTO select = commodityCategoryService.selectById(categoryId);
                         // 品牌
                         Long brandId = this.dealWithTheBrand(bo.getBrandName());
                         // 单位
                         String unitId = null;
                         if (CustomizeUtil.strIsNotNull(bo.getUnits())) {
                             unitId = this.processingUnit(bo.getUnits(), select.getName(), bo.getBrandName());
                             log.info("单位属性:{}", unitId);

                         }
                         // 销售属性
                         String salesIds = null;
                         if (CustomizeUtil.strIsNotNull(bo.getSales())) {
                             List<CategoryBrandAttributesVO> salesAttributesDate = categoryImportService.getSalesAttributesDate(bo.getSales(), select.getName(), bo.getBrandName());
                             if (CustomizeUtil.listIsNotNull(salesAttributesDate)) {
                                 List<String> ids = new ArrayList<>();
                                 for (CategoryBrandAttributesVO vo : salesAttributesDate) {
                                     ids.add(vo.getId().toString());
                                 }
                                 salesIds = getIds(Joiner.on(",").join(ids));
                                 log.info("销售属性:{}", salesIds);
                             }
                         }
                         String paramIds = null;
                         if (CustomizeUtil.strIsNotNull(bo.getParameter())) {
                             List<CategoryBrandAttributesVO> parameterAttributes = categoryImportService.getParameterAttributes(bo.getParameter(), select.getName(), bo.getBrandName());
                             if (CustomizeUtil.listIsNotNull(parameterAttributes)) {
                                 List<String> ids = new ArrayList<>();
                                 for (CategoryBrandAttributesVO vo : parameterAttributes) {
                                     ids.add(vo.getId().toString());
                                 }
                                 paramIds = getIds(Joiner.on(",").join(ids));
                                 log.info("参数属性:{}", paramIds);
                             }
                         }
                         String labelIds = null;
                         if (CustomizeUtil.strIsNotNull(bo.getLabels())) {
                             List<CategoryBrandAttributesVO> tagAttributes = categoryImportService.getTagAttributes(bo.getLabels(), select.getName(), bo.getBrandName());
                             if (CustomizeUtil.listIsNotNull(tagAttributes)) {
                                 List<String> ids = new ArrayList<>();
                                 for (CategoryBrandAttributesVO vo : tagAttributes) {
                                     ids.add(vo.getId().toString());
                                 }
                                 labelIds = getIds(Joiner.on(",").join(ids));
                                 log.info("标签属性:{}", labelIds);
                             }
                         }
                         // 查询类目品牌属性表
                         Wrapper<CategoryBrandAttributesRelationDTO> wrapper = new EntityWrapper<>();
                         wrapper.eq("category_id", categoryId).eq("brand_id", brandId);
                         CategoryBrandAttributesRelationDTO selectOne = categoryBrandAttributesRelationService.selectOne(wrapper);
                         if (selectOne == null) {
                             CategoryBrandAttributesRelationDTO insertDto = new CategoryBrandAttributesRelationDTO();
                             insertDto.setCategoryId(categoryId);
                             insertDto.setBrandId(brandId);
                             if (StringUtils.isNotBlank(salesIds)) {
                                 insertDto.setSpecificationIds(salesIds);
                             }
                             if (StringUtils.isNotBlank(paramIds)) {
                                 insertDto.setParamIds(paramIds);
                             }
                             if (StringUtils.isNotBlank(labelIds)) {
                                 insertDto.setLabelIds(labelIds);
                             }
                             if (CustomizeUtil.strIsNotNull(unitId)) {
                                 insertDto.setUnitIds(unitId);
                             }
                             insertDto.setId(ymlConfig.getPkId());
                             categoryBrandAttributesRelationService.insert(insertDto);
                         } else {
                             CategoryBrandAttributesRelationDTO updateDto = new CategoryBrandAttributesRelationDTO();
                             updateDto.setId(selectOne.getId());
                             // 处理单位ids
                             if (StringUtils.isNotBlank(unitId)) {
                                 String idsChange = this.handleIds(unitId, selectOne.getUnitIds());
                                 log.info("导入类目时单位id检测");
                                 updateDto.setUnitIds(idsChange);
                             }
                             // 处理销售属性ids
                             if (StringUtils.isNotBlank(salesIds)) {
                                 String idsChange = this.handleIds(salesIds, selectOne.getSpecificationIds());
                                 updateDto.setSpecificationIds(idsChange);
                             }
                             // 处理参数属性ids
                             if (StringUtils.isNotBlank(paramIds)) {
                                 String idsChange = this.handleIds(paramIds, selectOne.getParamIds());
                                 updateDto.setParamIds(idsChange);
                             }
                             // 处理标签属性ids
                             if (StringUtils.isNotBlank(labelIds)) {
                                 String idsChange = this.handleIds(labelIds, selectOne.getLabelIds());
                                 updateDto.setLabelIds(idsChange);
                             }
                             categoryBrandAttributesRelationService.updateById(updateDto);
                         }

                     } catch (Exception e) {
                         errorList.add(bo);
                         //   e.printStackTrace();
                         continue;
                     }
                 }
             }
        if (errorList!=null && errorList.size()>0){
            this.addImportErrorDate(errorList, list.get(0).getImportId());
            CategoryImportDTO updateDto = new CategoryImportDTO();
            updateDto.setId(list.get(0).getImportId());
            updateDto.setStatus(2);
            categoryImportService.updateById(updateDto);
           // log.info("类目导入正确数据处理失败，失败信息:{}", e.getMessage());
            redisUtil.del(CATEGORY_IMPORT_KEY);
        }else {
            CategoryImportDTO updateDto = new CategoryImportDTO();
            updateDto.setId(list.get(0).getImportId());
            updateDto.setStatus(1);
            categoryImportService.updateById(updateDto);
            log.info("类目导入正确数据处理成功");
            categoryImportService.updateById(updateDto);
            redisUtil.del(CATEGORY_IMPORT_KEY);
        }

    }

    /**
     * @Description: 处理品牌
     * @Author: chenyu.wang
     * @Params: [brandName]
     * @Date: 2020.12.16 16:39
     */
    private Long dealWithTheBrand(String brandName) {
        // 查询品牌是否存在
        Wrapper<BrandInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("name", brandName);
        BrandInfoDTO selectOne = brandInfoService.selectOne(wrapper);
        if (selectOne != null) {
            return selectOne.getId();
        }
        Long id = ymlConfig.getPkId();
        BrandInfoDTO insert = new BrandInfoDTO();
        insert.setId(id);
        insert.setNotes("类目导入时新增的品牌");
        insert.setName(brandName);
        insert.setInitials(PinYinUtils.getPinYinHeadCharFirstBrand(brandName));
        boolean flag = brandInfoService.insert(insert);
        if (!flag) {
            log.info("导入时新增品牌失败");
            throw new RuntimeException("导入时新增品牌失败");
        }
        return id;
    }

    /**
     * @Description: 处理单位
     * @Author: chenyu.wang
     * @Params: [unitName]
     * @Date: 2020.12.16 16:45"个;你" 测试8 卡高,RBU
     */
    private String processingUnit(String unitName, String categoryName, String brandName) {
        String note = categoryName + " " + brandName;
        String[] unitNames = unitName.split(";");
        List<String> ids = new ArrayList<>();
        HashSet<Long> idSet = new HashSet<>();
        for (String name : unitNames) {
            Wrapper<CommodityUnitDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("name", name);
            CommodityUnitDTO selectOne = commodityUnitService.selectOne(wrapper);
            if (selectOne != null) {
                boolean add = idSet.add(selectOne.getId());
                if (add) {
                    ids.add(selectOne.getId().toString());
                }
            } else {
                Long id = ymlConfig.getPkId();
                CommodityUnitDTO insert = new CommodityUnitDTO();
                insert.setId(id);
                insert.setCode(getlinkNo());
                insert.setNotes(note);
                insert.setName(name);
                boolean flag = commodityUnitService.insert(insert);
                if (!flag) {
                    log.info("类目导入时新增的单位失败");
                    throw new RuntimeException("类目导入时新增的单位失败");
                }
                boolean add = idSet.add(id);
                if (add) {
                    ids.add(id.toString());
                }
            }
        }
        return getIds(Joiner.on(",").join(ids));
    }

    /**
     * @Description: 处理类目
     * @Author: chenyu.wang
     * @Params: [bo]
     * @Date: 2020.12.16 15:31
     */
    private Long processingCategory(CategoryImportSaveBO bo) {
        // 先判定0级类目是否存在
        Wrapper<CommodityCategoryDTO> wrapperA = new EntityWrapper<>();
        wrapperA.eq("name", bo.getZeroCategoryName()).eq("parent_id", 0);
        CommodityCategoryDTO selectOne = commodityCategoryService.selectOne(wrapperA);
        if (selectOne == null) {
            // 不存在就新增0级类目
            Long idA = this.addCategory(0L, bo.getZeroCategoryName());
            // 新增1级类目(必填)
            Long idB = this.addCategory(idA, bo.getOneCategoryName());
            if (StringUtils.isBlank(bo.getTwoCategoryName())) {
                return idB;
            }
            // 新增2级类目
            Long idC = this.addCategory(idB, bo.getTwoCategoryName());
            if (StringUtils.isBlank(bo.getThreeCategoryName())) {
                return idC;
            }
            // 三级类目不为空直接返回
            return this.addCategory(idC, bo.getThreeCategoryName());
        }
        // 0级类目如果存在，判断1级类目是否存在
        Wrapper<CommodityCategoryDTO> wrapperB = new EntityWrapper<>();
        wrapperB.eq("name", bo.getOneCategoryName()).eq("parent_id", selectOne.getId());
        CommodityCategoryDTO selectB = commodityCategoryService.selectOne(wrapperB);
        if (selectB == null) {
            // 新增1级类目
            Long oneId = this.addCategory(selectOne.getId(), bo.getOneCategoryName());
            if (StringUtils.isBlank(bo.getTwoCategoryName())) {
                return oneId;
            }
            // 新增2级类目
            Long idC = this.addCategory(oneId, bo.getTwoCategoryName());
            if (StringUtils.isBlank(bo.getThreeCategoryName())) {
                return idC;
            }
            // 三级类目不为空直接返回
            return this.addCategory(idC, bo.getThreeCategoryName());
        }
        if (StringUtils.isBlank(bo.getTwoCategoryName())) {
            return selectB.getId();
        }
        // 检查一级类目是否是最终极且包含属性
        boolean checkCategory = this.isCheckCategory(selectB.getId());
        if (!checkCategory) {
            return 0L;
        }
        // 1级类目存在，判断2级类目是否存在
        Wrapper<CommodityCategoryDTO> wrapperC = new EntityWrapper<>();
        wrapperC.eq("name", bo.getTwoCategoryName()).eq("parent_id", selectB.getId());
        CommodityCategoryDTO selectC = commodityCategoryService.selectOne(wrapperC);
        if (selectC == null) {
            // 新增2级
            Long twoId = this.addCategory(selectB.getId(), bo.getTwoCategoryName());
            if (StringUtils.isBlank(bo.getThreeCategoryName())) {
                return twoId;
            }
            return this.addCategory(twoId, bo.getThreeCategoryName());
        }
        if (StringUtils.isBlank(bo.getThreeCategoryName())) {
            return selectC.getId();
        }
        // 检查二级类目是否是最终极且包含属性
        boolean checkCategoryA = this.isCheckCategory(selectC.getId());
        if (!checkCategoryA) {
            return 0L;
        }
        // 2级类目存在，判断3级类目是否存在
        Wrapper<CommodityCategoryDTO> wrapperD = new EntityWrapper<>();
        wrapperD.eq("name", bo.getThreeCategoryName()).eq("parent_id", selectC.getId());
        CommodityCategoryDTO selectD = commodityCategoryService.selectOne(wrapperD);
        if (selectD == null) {
            return this.addCategory(selectC.getId(), bo.getThreeCategoryName());
        }
        return selectD.getId();
    }

    /**
     * @return : boolean
     * @Author :chenyu.wang
     * @Description : 检查类目是否是最终级且包含属性
     * @Date : 17:37 2021.01.18
     * @Param : [categoryId]
     **/
    private boolean isCheckCategory(Long categoryId) {
        Integer checkCategory = commodityCategoryService.getCheckCategory(categoryId);
        if (checkCategory.equals(1)) {
            return true;
        }
        // 是最终级，检查是否包含属性
        Wrapper<CategoryBrandAttributesRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("category_id", categoryId);
        List<CategoryBrandAttributesRelationDTO> list = categoryBrandAttributesRelationService.selectList(wrapper);
        return CustomizeUtil.listIsNull(list);
    }

    /**
     * @Description: 添加类目并返回id
     * @Author: chenyu.wang
     * @Params: [pId, name]
     * @Date: 2020.12.16 16:05
     */
    private Long addCategory(Long pId, String name) {
        Long id = ymlConfig.getPkId();
        CommodityCategoryDTO insert = new CommodityCategoryDTO();
        insert.setName(name);
        insert.setInitials(PinYinUtils.getPinYinHeadCharFirstBrand(name));
        insert.setParentId(pId);
        insert.setCode(getlinkNo());
        insert.setId(id);
        boolean flag = commodityCategoryService.insert(insert);
        if (!flag) {
            log.info("保存类目失败");
            throw new RuntimeException("保存类目失败");
        }
        return id;
    }

    /**
     * @Description: 修改类目属性
     * @Author: chenyu.wang
     * @Params: [dto, saveBO, categoryName, brandName]
     * @Date: 2020.12.07 09:28
     */
    private Map<String, String> update(CategoryBrandAttributesRelationDTO dto, CategoryMaintainNewSaveBO saveBO, String categoryName, String brandName) {
        log.info("修改类目属性:{}", saveBO);
        CategoryBrandAttributesRelationDTO updateD = new CategoryBrandAttributesRelationDTO();
        updateD.setId(dto.getId());
        // 销售属性
        List<CommoditySpecificationVO> salesAttributeCollection = saveBO.getSalesAttributeCollection();
        String salesAttributesIds = this.processingSalesAttributes(salesAttributeCollection, categoryName, brandName);
        if (StringUtils.isNotBlank(salesAttributesIds)) {
            String idsChange = this.handleIds(salesAttributesIds, dto.getSpecificationIds());
            log.info("标签属性:{}", idsChange);
            updateD.setSpecificationIds(idsChange);
        }
        // 参数属性
        List<CategoryParamBean> parameterAttributeCollectionA = saveBO.getParameterAttributeCollection();
        String parameterAttributesIds = this.processingParameterAttributes(parameterAttributeCollectionA, categoryName, brandName);
        if (StringUtils.isNotBlank(parameterAttributesIds)) {
            String idsChange = this.handleIds(parameterAttributesIds, dto.getParamIds());
            log.info("参数属性:{}", idsChange);
            updateD.setParamIds(idsChange);
        }
        // 单位属性
        List<CommodityUnitDTO> unitCollectionA = saveBO.getUnitCollection();
        String unitAttributeIds = this.getUnitAttribute(unitCollectionA, categoryName, brandName);
        if (StringUtils.isNotBlank(unitAttributeIds)) {
            String idsChange = this.handleIds(unitAttributeIds, dto.getUnitIds());
            log.info("单位属性:{}", idsChange);
            updateD.setUnitIds(idsChange);
        }
        // 标签属性
        List<CommodityLabelDTO> labelCollectionA = saveBO.getLabelCollection();
        String labelAttributesIds = this.getLabelAttributes(labelCollectionA, categoryName, brandName);
        if (StringUtils.isNotBlank(labelAttributesIds)) {
            String idsChange = this.handleIds(labelAttributesIds, dto.getLabelIds());
            log.info("标签属性:{}", idsChange);
            updateD.setLabelIds(idsChange);
        }
        categoryBrandAttributesRelationService.updateById(updateD);
        Map<String, String> map = new HashMap<>();
        map.put("salesIds", salesAttributesIds);
        map.put("parameterIds", parameterAttributesIds);
        map.put("unitIds", unitAttributeIds);
        map.put("labelIds", labelAttributesIds);
        return map;
    }

    /**
     * @Description: 添加类目属性
     * @Author: chenyu.wang
     * @Params: [saveBO, categoryName, brandName]
     * @Date: 2020.12.07 09:28
     */
    private Map<String, String> add(CategoryMaintainNewSaveBO saveBO, String categoryName, String brandName, Long categoryId) {
        Long id = ymlConfig.getPkId();
        CategoryBrandAttributesRelationDTO insertD = new CategoryBrandAttributesRelationDTO();
        insertD.setId(id);
        insertD.setCategoryId(categoryId);
        insertD.setBrandId(saveBO.getBrandId());
        // 销售属性
        List<CommoditySpecificationVO> salesAttributeCollectionA = saveBO.getSalesAttributeCollection();
        String salesAttributesIds = this.processingSalesAttributes(salesAttributeCollectionA, categoryName, brandName);
        if (StringUtils.isNotBlank(salesAttributesIds)) {
            insertD.setSpecificationIds(salesAttributesIds);
        }
        // 参数属性
        List<CategoryParamBean> parameterAttributeCollectionA = saveBO.getParameterAttributeCollection();
        String parameterAttributesIds = this.processingParameterAttributes(parameterAttributeCollectionA, categoryName, brandName);
        if (StringUtils.isNotBlank(parameterAttributesIds)) {
            insertD.setParamIds(parameterAttributesIds);
        }
        // 单位属性
        List<CommodityUnitDTO> unitCollectionA = saveBO.getUnitCollection();
        String unitAttributeIds = this.getUnitAttribute(unitCollectionA, categoryName, brandName);
        if (StringUtils.isNotBlank(unitAttributeIds)) {
            insertD.setUnitIds(unitAttributeIds);
        }
        // 标签属性
        List<CommodityLabelDTO> labelCollectionB = saveBO.getLabelCollection();
        String labelAttributesIds = this.getLabelAttributes(labelCollectionB, categoryName, brandName);
        if (StringUtils.isNotBlank(labelAttributesIds)) {
            insertD.setLabelIds(labelAttributesIds);
        }
        categoryBrandAttributesRelationService.insert(insertD);
        log.info("添加类目属性:{}", saveBO);
        Map<String, String> map = new HashMap<>();
        map.put("salesIds", salesAttributesIds);
        map.put("parameterIds", parameterAttributesIds);
        map.put("unitIds", unitAttributeIds);
        map.put("labelIds", labelAttributesIds);
        return map;
    }

    /**
     * @Description: 处理销售属性
     * @Author: chenyu.wang
     * @Params: [list, categoryName, brandName]
     * @Date: 2020.12.04 15:01
     */
    private String processingSalesAttributes(List<CommoditySpecificationVO> list, String categoryName, String brandName) {
        String notes = categoryName + " " + brandName;
        if (isNull(list)) {
            return null;
        }
        List<String> ids = new ArrayList<>();
        for (CommoditySpecificationVO specificationVO : list) {
            if (specificationVO.getId() != null) {
                ids.add(specificationVO.getId().toString());
                List<CommoditySpecificationAttrDTO> attrList = specificationVO.getAttrList();
                for (CommoditySpecificationAttrDTO attrDTO : attrList) {
                    if (attrDTO.getId() == null) {
                        // 先查询是否存在相同的属性值
                        Wrapper<CommoditySpecificationAttrDTO> wrapper = new EntityWrapper<>();
                        wrapper.eq("specification_id", specificationVO.getId()).eq("attr_name", attrDTO.getAttrName());
                        CommoditySpecificationAttrDTO selectOne = commoditySpecificationAttrService.selectOne(wrapper);
                        if (selectOne == null) {
                            CommoditySpecificationAttrDTO insertA = new CommoditySpecificationAttrDTO();
                            insertA.setId(ymlConfig.getPkId());
                            insertA.setSpecificationId(specificationVO.getId());
                            insertA.setAttrName(attrDTO.getAttrName());
                            insertA.setStatus(0);
                            insertA.setIsPublish(1);
                            String clazz = JSONObject.toJSONString(insertA);
                            this.allAdd(clazz, 1);
                        }
                    }
                }
            } else {
                Long idB = ymlConfig.getPkId();
                ids.add(idB.toString());
                CommoditySpecificationDTO inertB = new CommoditySpecificationDTO();
                inertB.setName(specificationVO.getName());
                inertB.setStatus(0);
                inertB.setIsPublish(1);
                inertB.setNotes(notes);
                inertB.setId(idB);
                String objA = JSONObject.toJSONString(inertB);
                this.allAdd(objA, 0);
                List<CommoditySpecificationAttrDTO> attrList = specificationVO.getAttrList();
                for (CommoditySpecificationAttrDTO specificationAttrDTO : attrList) {
                    CommoditySpecificationAttrDTO insertA = new CommoditySpecificationAttrDTO();
                    insertA.setId(ymlConfig.getPkId());
                    insertA.setSpecificationId(idB);
                    insertA.setAttrName(specificationAttrDTO.getAttrName());
                    insertA.setStatus(0);
                    insertA.setIsPublish(1);
                    String clazz = JSONObject.toJSONString(insertA);
                    this.allAdd(clazz, 1);
                }

            }
        }
        if (isNull(ids)) {
            throw new RuntimeException("销售属性ids为空!");
        }
        return getIds(Joiner.on(",").join(ids));
    }

    /**
     * @Description: 处理参数属性
     * @Author: chenyu.wang
     * @Params: [list, categoryName, brandName]
     * @Date: 2020.12.04 15:31
     */
    private String processingParameterAttributes(List<CategoryParamBean> list, String categoryName, String brandName) {
        String notes = categoryName + " " + brandName;
        if (isNull(list)) {
            return null;
        }
        List<String> ids = new ArrayList<>();
        for (CategoryParamBean paramBean : list) {
            if (paramBean.getId() != null) {
                ids.add(paramBean.getId().toString());
                List<CommodityParamValueDTO> valueList = paramBean.getValueList();
                for (CommodityParamValueDTO valueDTO : valueList) {
                    if (valueDTO.getId() == null) {
                        // 先根据参数id和值名称查询是否存在
                        Wrapper<CommodityParamValueDTO> wrapper = new EntityWrapper<>();
                        wrapper.eq("param_id", paramBean.getId()).eq("value_name", valueDTO.getValueName());
                        CommodityParamValueDTO selectOne = commodityParamValueService.selectOne(wrapper);
                        if (selectOne == null) {
                            CommodityParamValueDTO insertA = new CommodityParamValueDTO();
                            insertA.setParamId(paramBean.getId());
                            insertA.setValueName(valueDTO.getValueName());
                            insertA.setId(ymlConfig.getPkId());
                            String clazz = JSONObject.toJSONString(insertA);
                            this.allAdd(clazz, 3);
                        }
                    }
                }
            } else {
                Long id = ymlConfig.getPkId();
                CommodityParamDTO insertB = new CommodityParamDTO();
                insertB.setParamName(paramBean.getParamName());
                insertB.setStatus(0);
                insertB.setIsPublish(1);
                insertB.setNotes(notes);
                insertB.setId(id);
                String clazz = JSONObject.toJSONString(insertB);
                this.allAdd(clazz, 2);
                ids.add(id.toString());
                List<CommodityParamValueDTO> valueList = paramBean.getValueList();
                for (CommodityParamValueDTO valueDTO : valueList) {
                    CommodityParamValueDTO insertA = new CommodityParamValueDTO();
                    insertA.setParamId(id);
                    insertA.setValueName(valueDTO.getValueName());
                    insertA.setId(ymlConfig.getPkId());
                    String clazzA = JSONObject.toJSONString(insertA);
                    this.allAdd(clazzA, 3);
                }

            }
        }
        if (isNull(ids)) {
            throw new RuntimeException("参数信息为空");
        }
        return getIds(Joiner.on(",").join(ids));
    }

    /**
     * @Description: 处理单位属性
     * @Author: chenyu.wang
     * @Params: [list, categoryName, brandName]
     * @Date: 2020.12.04 15:33
     */
    private String getUnitAttribute(List<CommodityUnitDTO> list, String categoryName, String brandName) {
        String notes = categoryName + " " + brandName;
        if (isNull(list)) {
            return null;
        }
        List<String> ids = new ArrayList<>();
        for (CommodityUnitDTO unitDTO : list) {
            if (unitDTO.getId() != null) {
                ids.add(unitDTO.getId().toString());
            } else {
                // 先查询是否存在相同单位
                Wrapper<CommodityUnitDTO> wrapper = new EntityWrapper<>();
                wrapper.eq("name", unitDTO.getName());
                CommodityUnitDTO selectOne = commodityUnitService.selectOne(wrapper);
                if (selectOne == null) {
                    Long id = ymlConfig.getPkId();
                    CommodityUnitDTO insertA = new CommodityUnitDTO();
                    insertA.setName(unitDTO.getName());
                    insertA.setStatus(0);
                    insertA.setIsPublish(1);
                    insertA.setNotes(notes);
                    insertA.setId(id);
                    String clazz = JSONObject.toJSONString(insertA);
                    this.allAdd(clazz, 4);
                    ids.add(id.toString());
                } else {
                    ids.add(selectOne.getId().toString());
                }
            }
        }
        if (isNull(ids)) {
            throw new RuntimeException("无单位属性");
        }
        return getIds(Joiner.on(",").join(ids));
    }

    /**
     * @Description: 处理标签属性
     * @Author: chenyu.wang
     * @Params: [list, categoryName, brandName]
     * @Date: 2020.12.04 15:42
     */
    private String getLabelAttributes(List<CommodityLabelDTO> list, String categoryName, String brandName) {
        String notes = categoryName + " " + brandName;
        if (isNull(list)) {
            return null;
        }
        List<String> ids = new ArrayList<>();
        for (CommodityLabelDTO labelDTO : list) {
            if (labelDTO.getId() != null) {
                ids.add(labelDTO.getId().toString());
            } else {
                // 查询是否存在标签
                Wrapper<CommodityLabelDTO> wrapper = new EntityWrapper<>();
                wrapper.eq("name", labelDTO.getName());
                CommodityLabelDTO selectOne = commodityLabelService.selectOne(wrapper);
                if (selectOne != null) {
                    ids.add(selectOne.getId().toString());
                } else {
                    Long id = ymlConfig.getPkId();
                    CommodityLabelDTO insertB = new CommodityLabelDTO();
                    insertB.setName(labelDTO.getName());
                    insertB.setStatus(0);
                    insertB.setNotes(notes);
                    insertB.setId(id);
                    String clazz = JSONObject.toJSONString(insertB);
                    this.allAdd(clazz, 5);
                    ids.add(id.toString());
                }
            }
        }
        if (isNull(ids)) {
            throw new RuntimeException("无标签属性");
        }
        return getIds(Joiner.on(",").join(ids));
    }

    /**
     * @Description: 异步保存数据
     * @Author: chenyu.wang
     * @Params: [clazz, type]
     * @Date: 2020.12.04 15:12
     */
    @Async
    public void allAdd(String clazz, Integer type) {
        // type:0添加属性，1添加属性值，2添加参数，3添加参数值，4添加单位，5添加标签
        boolean result = false;
        switch (type) {
            case 0:
                CommoditySpecificationDTO inertA = JSONObject.parseObject(clazz, CommoditySpecificationDTO.class);
                result = commoditySpecificationService.insert(inertA);
                log.info("添加属性状态 : {}", result);
                break;
            case 1:
                CommoditySpecificationAttrDTO inertB = JSONObject.parseObject(clazz, CommoditySpecificationAttrDTO.class);
                result = commoditySpecificationAttrService.insert(inertB);
                log.info("添加属性值状态 : {}", result);
                break;
            case 2:
                CommodityParamDTO inertC = JSONObject.parseObject(clazz, CommodityParamDTO.class);
                result = commodityParamService.insert(inertC);
                log.info("添加参数状态 : {}", result);
                break;
            case 3:
                CommodityParamValueDTO insertC = JSONObject.parseObject(clazz, CommodityParamValueDTO.class);
                result = commodityParamValueService.insert(insertC);
                log.info("添加参数值状态 : {}", result);
                break;
            case 4:
                CommodityUnitDTO insertD = JSONObject.parseObject(clazz, CommodityUnitDTO.class);
                result = commodityUnitService.insert(insertD);
                log.info("添加单位状态 : {}", result);
                break;
            case 5:
                CommodityLabelDTO insertE = JSONObject.parseObject(clazz, CommodityLabelDTO.class);
                result = commodityLabelService.insert(insertE);
                log.info("添加标签状态 : {}", result);
                break;
            default:
                break;
        }
    }

    private String handleIds(String now, String original) {
        // 原来的ids为空则直接返回现在的ids
        if (CustomizeUtil.strIsNull(original)) {
            return now;
        }
        HashSet<String> result = new HashSet<>();
        // 原来的ids
        String[] splitA = original.split(",");
        // 现在的ids
        String[] splitB = now.split(",");
        for (String sA : splitA) {
            boolean add = result.add(sA);
            log.info("添加原来的ids状态:{}", add);
        }
        for (String sB : splitB) {
            boolean add = result.add(sB);
            log.info("添加现在的ids状态:{}", add);
        }
        String join = Joiner.on(",").join(result);
        log.info("最终的ids:{}", join);
        return getIds(join);
    }

    /**
     * @Description: 获取excel数据时对数据类型的判定及处理
     * @Author: chenyu.wang
     * @Params: [cell]
     * @Date: 2020.11.24 11:20
     */
    private String getCellValue(Cell cell) {
        String cellValue = "";
        if (cell == null) {
            return cellValue;
        }
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                cellValue = cell.getStringCellValue().trim();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                DecimalFormat df = new DecimalFormat("0");

                cellValue = df.format(cell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                cellValue = cell.getCellFormula();
                break;
            default:
                cellValue = "";
        }
        return cellValue;
    }

}
